package com.jci.vsd.activity.enterprise;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jci.vsd.R;
import com.jci.vsd.activity.BaseActivity;
import com.jci.vsd.activity.LoginActivity;
import com.jci.vsd.adapter.enterprise.MembersManagerAdapter;
import com.jci.vsd.bean.enterprise.AjustMembersBean;
import com.jci.vsd.bean.enterprise.MembersBean;
import com.jci.vsd.network.control.MembersManageControl;
import com.jci.vsd.observer.CommonDialogObserver;
import com.jci.vsd.observer.RxHelper;
import com.jci.vsd.utils.Loger;
import com.jci.vsd.view.widget.SimpleToast;
import com.jci.vsd.view.widget.treelist.TreeMenuBaseAdapter;
import com.jci.vsd.view.widget.treelist.bean.Node;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Created by liqing on 18/6/29.
 * 成员选择
 */

public class MembsersSelectActivity extends BaseActivity {


    @BindView(R.id.lv_members)
    ListView lvMembers;
    private MembersManagerAdapter<MembersBean> adapter;
    private List<MembersBean> mData;
    private List<MembersBean> departmentList;//部门成员集合

    @BindView(R.id.button_back)
    ImageButton backBtn;
    @BindView(R.id.textview_title)
    TextView titleTxt;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members_manage);

        mData = new ArrayList<>();
        departmentList = new ArrayList<>();
        // initTestData();
        initViewEvent();
        loadData();

    }

    private void initTreeMemberAdapter() {
        try {
            adapter = new MembersManagerAdapter<>(lvMembers, this, mData, 1);
            adapter.setListExpand(true);
            adapter.setTreeNodeClickListener(new TreeMenuBaseAdapter.OnTreeNodeClickListener() {
                @Override
                public void onNodeClick(Node node, int position) {
                    String pid = node.getParentID();
                    Loger.e("---nodeclickPos--" + position + "pid" + pid);
                    if (node.getChildrenNodes().size() == 0 && !pid.equals("0")) {
                        MembersBean bean = mData.get(position);

                        if (bean != null) {
                            Loger.e("---nodeSlect" + "pos" + position + bean.getName() + "-pid" + bean.getPid() + "--id" + bean.getId());

                            if (bean.getLeader() != null && bean.getLeader()) {
                                SimpleToast.toastMessage("已经是部门主管，不可以重复设置", Toast.LENGTH_SHORT);
                                return;
                            } else if (bean.getLeader() == null) {
                                SimpleToast.toastMessage("设置非法", Toast.LENGTH_SHORT);
                                return;
                            }

                        }
                        MembersBean selectBean  =new MembersBean();
                        selectBean.setId(node.getUid());
                        selectBean.setName(node.getNodeName());
                        Intent mIntent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("user",selectBean);
                        Loger.e("--selectBean" + selectBean.getName() + "---id=" + selectBean.getId());
                        mIntent.putExtras(bundle);
                        // 设置结果，并进行传送
                        setResult(Activity.RESULT_OK, mIntent);
                        finish();
                    }

                }

            });

            adapter.setTreeNodeLongClickListener(new TreeMenuBaseAdapter.OnTreeNodeLongClickListener() {
                @Override
                public void onNodeLongClick(Node node, int position) {
                }
            });
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        lvMembers.setAdapter(adapter);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void initViewEvent() {
        backBtn.setOnClickListener(this);
        titleTxt.setText(getResources().getString(R.string.select_members));
    }


    private void initTestData() {
        //parentItem
//        mData.add(new MembersBean("1", "0", "总经理办公室"));
//        mData.add(new MembersBean("2", "0", "销售部"));
//        //总经理下的childItem
//        mData.add(new MembersBean("3", "1", "张三"));
//        mData.add(new MembersBean("4", "1", "王一"));
//
//        //销售部下的
//
//        mData.add(new MembersBean("5", "2", "孙田"));
//        mData.add(new MembersBean("6", "2", "Jsec"));
//        mData.add(new MembersBean("7", "2", "Lotey"));

    }


    //加载MemberBean
    private void loadData() {
        Observable<List<MembersBean>> observable = new MembersManageControl().getMembers();
        CommonDialogObserver<List<MembersBean>> observer = new CommonDialogObserver<List<MembersBean>>(this) {
            @Override
            public void onNext(List<MembersBean> beanList) {
                super.onNext(beanList);
                mData.clear();
                mData.addAll(beanList);
                initTreeMemberAdapter();

                for (int i = 0; i < mData.size(); i++) {
                    if (mData.get(i).getPid().equals("0")) {
                        departmentList.add(mData.get(i));
                    }
                }


            }


            @Override
            public void onError(Throwable t) {
                super.onError(t);
            }
        };
        RxHelper.bindOnUIActivityLifeCycle(observable, observer, MembsersSelectActivity.this);
    }



}
